package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.pojo.dto.MenuSettingLocationDTO;
import com.xbongbong.paas.pojo.dto.SharePermissionWhiteDTO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.vo.ScriptVO;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author 李翔
 * @date 2023/4/17
 * @description
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX+"/script/sharePermission")
public class SharePermissionController {
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private PaasRedisHelper paasRedisHelper;


    @RequestMapping(value = "/setWhiteList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setWhiteList(@RequestBody @Valid SharePermissionWhiteDTO sharePermissionWhiteDTO, BindingResult br) throws XbbException {
        XbbResponse<ScriptVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (!proBaseConfig.getXbbCode().equals(sharePermissionWhiteDTO.getXbbCode())) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Set<String> updateCorpidList = new HashSet<>();
                List<String> businessRuleCorpid = sharePermissionWhiteDTO.getCorpidList();
                CompanyConfigEntity companyConfigEntity = companyConfigModel.getByConfigAlias(CompanyConfigEnum.SHARE_PERMISSION_WHITE_CORPIDS.getAlias(), "0");
                if (Objects.isNull(companyConfigEntity)) {
                    updateCorpidList.addAll(businessRuleCorpid);
                } else {
                    List<String> list = JSON.parseArray(companyConfigEntity.getConfigValue(), String.class);
                    if (Objects.equals(1, sharePermissionWhiteDTO.getOperationType())) {
                        list.removeAll(businessRuleCorpid);
                    } else {
                        list.addAll(businessRuleCorpid);
                    }
                    updateCorpidList.addAll(list);
                }
                companyConfigModel.save("0", CompanyConfigEnum.SHARE_PERMISSION_WHITE_CORPIDS.getAlias(), CompanyConfigEnum.SHARE_PERMISSION_WHITE_CORPIDS.getName(), JSON.toJSONString(updateCorpidList));
                paasRedisHelper.setValue(RedisPrefixConstant.SHARE_PERMISSION_WHITE_CORPIDS, CompanyConfigEnum.SHARE_PERMISSION_WHITE_CORPIDS.getAlias(), JSON.toJSONString(updateCorpidList), TimeConstant.SECONDS_PER_DAY);
                ScriptVO scriptVO = new ScriptVO();
                response = new XbbResponse<>(scriptVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
